{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>instant</th>\n",
       "      <th>season_1</th>\n",
       "      <th>season_2</th>\n",
       "      <th>season_3</th>\n",
       "      <th>season_4</th>\n",
       "      <th>mnth_1</th>\n",
       "      <th>mnth_2</th>\n",
       "      <th>mnth_3</th>\n",
       "      <th>mnth_4</th>\n",
       "      <th>mnth_5</th>\n",
       "      <th>...</th>\n",
       "      <th>workingday_1</th>\n",
       "      <th>weathersit_1</th>\n",
       "      <th>weathersit_2</th>\n",
       "      <th>weathersit_3</th>\n",
       "      <th>temp</th>\n",
       "      <th>atemp</th>\n",
       "      <th>hum</th>\n",
       "      <th>windspeed</th>\n",
       "      <th>yr</th>\n",
       "      <th>cnt</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>366</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.387359</td>\n",
       "      <td>0.389264</td>\n",
       "      <td>0.712082</td>\n",
       "      <td>0.350001</td>\n",
       "      <td>1</td>\n",
       "      <td>2294</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>367</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.266546</td>\n",
       "      <td>0.227394</td>\n",
       "      <td>0.392086</td>\n",
       "      <td>0.633460</td>\n",
       "      <td>1</td>\n",
       "      <td>1951</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>368</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.113228</td>\n",
       "      <td>0.061963</td>\n",
       "      <td>0.453728</td>\n",
       "      <td>0.707688</td>\n",
       "      <td>1</td>\n",
       "      <td>2236</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>369</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0.060271</td>\n",
       "      <td>0.052856</td>\n",
       "      <td>0.426306</td>\n",
       "      <td>0.334607</td>\n",
       "      <td>1</td>\n",
       "      <td>2368</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>370</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.257562</td>\n",
       "      <td>0.261664</td>\n",
       "      <td>0.538989</td>\n",
       "      <td>0.221813</td>\n",
       "      <td>1</td>\n",
       "      <td>3272</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 37 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   instant  season_1  season_2  season_3  season_4  mnth_1  mnth_2  mnth_3  \\\n",
       "0      366         1         0         0         0       1       0       0   \n",
       "1      367         1         0         0         0       1       0       0   \n",
       "2      368         1         0         0         0       1       0       0   \n",
       "3      369         1         0         0         0       1       0       0   \n",
       "4      370         1         0         0         0       1       0       0   \n",
       "\n",
       "   mnth_4  mnth_5  ...   workingday_1  weathersit_1  weathersit_2  \\\n",
       "0       0       0  ...              0             1             0   \n",
       "1       0       0  ...              0             1             0   \n",
       "2       0       0  ...              1             1             0   \n",
       "3       0       0  ...              1             0             1   \n",
       "4       0       0  ...              1             1             0   \n",
       "\n",
       "   weathersit_3      temp     atemp       hum  windspeed  yr   cnt  \n",
       "0             0  0.387359  0.389264  0.712082   0.350001   1  2294  \n",
       "1             0  0.266546  0.227394  0.392086   0.633460   1  1951  \n",
       "2             0  0.113228  0.061963  0.453728   0.707688   1  2236  \n",
       "3             0  0.060271  0.052856  0.426306   0.334607   1  2368  \n",
       "4             0  0.257562  0.261664  0.538989   0.221813   1  3272  \n",
       "\n",
       "[5 rows x 37 columns]"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#导入必要工具包读入数据\n",
    "import pandas as pd\n",
    "import numpy as  np\n",
    "from sklearn.linear_model import LinearRegression,RidgeCV,LassoCV,ElasticNetCV\n",
    "from sklearn.metrics import mean_squared_error\n",
    "from sklearn.metrics import r2_score\n",
    "import matplotlib.pyplot as plt\n",
    "train=pd.read_csv('FE_data_train.csv')\n",
    "train.head()\n",
    "test=pd.read_csv('FE_data_test.csv')    \n",
    "test.head()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "train:(365, 37)\n",
      "test:(366, 37)\n"
     ]
    }
   ],
   "source": [
    "y_train=train[\"cnt\"]\n",
    "y_test=test[\"cnt\"]\n",
    "x_train=train.drop([\"cnt\"],axis=1)\n",
    "x_test=test.drop([\"cnt\"],axis=1)\n",
    "test_ID=test[\"instant\"]\n",
    "x_train.drop(['instant','yr'],axis=1,inplace=True)\n",
    "x_test.drop(['instant','yr'],axis=1,inplace=True)\n",
    "print(\"train:\"+ str(train.shape))\n",
    "\n",
    "print(\"test:\"+ str(test.shape))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "最小二乘线性回归"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('mean_y', 3405.7616438356163)\n",
      "('mean_train_y', 1.5914175510574313)\n",
      "('mean_train_y', -1.3353093364669692e-16)\n",
      "('mean y diff :', 1.5914175510574315)\n",
      "('rsme train : ', 0.3984288343324977)\n",
      "('rsme test:', 0.7486328465023797)\n",
      "('r2_score on Training set :', 0.8408183498624813)\n",
      "('r2_score on Test set :', 0.6660819259791522)\n"
     ]
    }
   ],
   "source": [
    "#数据标准化\n",
    "mean_y = y_train.mean()\n",
    "print(\"mean_y\",mean_y)\n",
    "std_y = y_train.std()\n",
    "y_train = (y_train - mean_y)/std_y\n",
    "\n",
    "y_test = (y_test - mean_y)/std_y\n",
    "\n",
    "mean_test_y = y_test.mean()\n",
    "print(\"mean_train_y\",mean_test_y)\n",
    "mean_train_y = y_train.mean()\n",
    "print(\"mean_train_y\",mean_train_y)\n",
    "mean_diff = mean_test_y -  mean_train_y\n",
    "\n",
    "print(\"mean y diff :\", mean_diff)\n",
    "\n",
    "\n",
    "#生成学习器实例\n",
    "lr=LinearRegression()\n",
    "#训练学习器\n",
    "lr.fit(x_train,y_train)\n",
    "#获得训练误差\n",
    "y_train_pred=lr.predict(x_train)\n",
    "y_test_pred=lr.predict(x_test)\n",
    "y_test_pred+=mean_diff\n",
    "rsme_train=np.sqrt(mean_squared_error(y_train,y_train_pred))\n",
    "rsme_test=np.sqrt(mean_squared_error(y_test,y_test_pred))\n",
    "print(\"rsme train : \",rsme_train)\n",
    "print(\"rsme test:\",rsme_test)\n",
    "r2_train=r2_score(y_train,y_train_pred)\n",
    "r2_test=r2_score(y_test,y_test_pred)\n",
    "print(\"r2_score on Training set :\", r2_train)\n",
    "print(\"r2_score on Test set :\", r2_test)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. 生成学习器实例"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('Best alpha :', 1.0)\n",
      "('cv of rmse :', 0.43459878998220386)\n",
      "('RMSE on Training set :', 0.40015402954530344)\n",
      "('RMSE on Test set :', 0.7507837586620438)\n",
      "('r2_score on Training set :', 0.8394368536074439)\n",
      "('r2_score on Test set :', 0.6641603960130702)\n"
     ]
    }
   ],
   "source": [
    "alphas = [0.01, 0.1, 1, 10, 100, 1000]\n",
    "ridge = RidgeCV(alphas = alphas,store_cv_values=True )\n",
    "# 2. 用训练数据度模型进行训练\n",
    "# RidgeCV采用的是广义交叉验证（Generalized Cross-Validation），留一交叉验证（N-折交叉验证）的一种有效实现方式\n",
    "ridge.fit(x_train, y_train)\n",
    "#通过交叉验证得到的最佳超参数alpha\n",
    "alpha = ridge.alpha_\n",
    "print(\"Best alpha :\", alpha)\n",
    "\n",
    "# 交叉验证估计的测试误差\n",
    "mse_cv = np.mean(ridge.cv_values_, axis = 0)\n",
    "rmse_cv = np.sqrt(mse_cv)\n",
    "print(\"cv of rmse :\", min(rmse_cv))\n",
    "#训练上测试，训练误差，实际任务中这一步不需要\n",
    "y_train_pred = ridge.predict(x_train)\n",
    "rmse_train = np.sqrt(mean_squared_error(y_train,y_train_pred))\n",
    "\n",
    "y_test_pred = ridge.predict(x_test)\n",
    "y_test_pred += mean_diff\n",
    "\n",
    "\n",
    "\n",
    "rmse_test = np.sqrt(mean_squared_error(y_test,y_test_pred))\n",
    "\n",
    "print(\"RMSE on Training set :\", rmse_train)\n",
    "print(\"RMSE on Test set :\", rmse_test)\n",
    "r2_score_train = r2_score(y_train,y_train_pred)\n",
    "r2_score_test = r2_score(y_test,y_test_pred)\n",
    "print(\"r2_score on Training set :\", r2_score_train)\n",
    "print(\"r2_score on Test set :\", r2_score_test)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "lasso 回归"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('Best alpha :', 0.0005827919419717718)\n",
      "('RMSE on Training set :', 0.3987718430746961)\n",
      "('RMSE on Test set :', 0.7481777693576163)\n",
      "('r2_score on Training set :', 0.8405441518333672)\n",
      "('r2_score on Test set :', 0.6664877652322392)\n"
     ]
    }
   ],
   "source": [
    "#生成实例\n",
    "lasso = LassoCV()\n",
    "lasso.fit(x_train, y_train)\n",
    "alpha = lasso.alpha_\n",
    "print(\"Best alpha :\", alpha)\n",
    "y_train_pred = lasso.predict(x_train)\n",
    "rmse_train = np.sqrt(mean_squared_error(y_train,y_train_pred))\n",
    "print(\"RMSE on Training set :\", rmse_train)\n",
    "y_test_pred = lasso.predict(x_test)\n",
    "y_test_pred += mean_diff\n",
    "\n",
    "rmse_test = np.sqrt(mean_squared_error(y_test,y_test_pred))\n",
    "print(\"RMSE on Test set :\", rmse_test)\n",
    "r2_score_train = r2_score(y_train,y_train_pred)\n",
    "r2_score_test = r2_score(y_test,y_test_pred)\n",
    "print(\"r2_score on Training set :\", r2_score_train)\n",
    "print(\"r2_score on Test set :\", r2_score_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": [
    "y_test_pred = lasso.predict(x_test)\n",
    "y_test_pred += mean_diff\n",
    "y_test_pred = y_test_pred * std_y +  mean_y\n",
    "\n",
    "#生成提交测试结果\n",
    "\n",
    "df = pd.DataFrame({\"instant\":test_ID, 'cnt':y_test_pred})\n",
    "#df.reindex(columns=['instant'])\n",
    "#y = pd.Series(data = y_test_pred, name = 'cnt')\n",
    "#df = pd.concat([testID, y], axis = 1, ignore_index=True)\n",
    "df.to_csv('submission.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.14"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
